package com.geen.tuoke.network.utils

import com.geen.tuoke.bean.UserInfo
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV


object CacheUtil {

    var mmkv: MMKV = MMKV.mmkvWithID("app")

    fun saveToken(token: String) {
        mmkv.encode("token", token)
    }

    fun getToken(): String {
        return mmkv.decodeString("token", "")
    }

    fun savePhoneNumber(phone: String) {
        mmkv.encode("phone", phone)
    }

    fun getPhoneNumber(): String {
        return mmkv.decodeString("phone", "")
    }

    fun saveUserInfo(userInfo: UserInfo?) {
        mmkv.encode("userInfo", Gson().toJson(userInfo))
    }

    fun getUserInfo(): UserInfo? {
        val userInfoStr = mmkv.getString("userInfo", "")
        return Gson().fromJson(userInfoStr, UserInfo::class.java)
    }

    fun clearCache(){
        mmkv.clearAll()
    }


    inline fun <reified T> genericType() = object : TypeToken<T>() {}.type
}